package com.heima.kafka.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 胡慧
 * @title: HelloController
 * @projectName heima-leadnews
 * @description: TODO
 * @date 2021/2/2417:29
 */
@RestController
public class HelloController {
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @GetMapping("/hello")
    public String hello(){
        //传递消息为对象
        User user = new User();
        user.setUsername("zhangsan");
        user.setAge(18);
        //第一个参数：topics
        //第二个参数：消息内容
        //kafkaTemplate.send("kafka-hello","黑马程序员");
        //转换成json格式
        kafkaTemplate.send("kafka-hello", JSON.toJSONString(user));
        return "ok";
    }
}
